﻿using lBLL;
using lDAL;
using Model;

namespace BLL
{
    public class UserBLL: lUserBLL
    {
        public UserBLL(lUserDAL<Userinfor> db , lUserDAL<Hoteinfor> dbs)
        {
            Db = db;
            Dbs = dbs;
        }

        public lDAL.lUserDAL<Userinfor> Db { get; }
        public lUserDAL<Hoteinfor> Dbs { get; }

        public Task<int> AddAsync(Userinfor userinfor)
        {
            return Db.AddAsync(userinfor);
        }

        public Task<int> DeleteAsync(int id)
        {
            var a = Db.GetAllAsync().FirstOrDefault();
            return Db.DeleteAsync(a);
        }

        public Userinfor Fan(int id)
        {
            return Db.GetAllAsync().FirstOrDefault(x=>x.UserId==id);
        }

        public dynamic GetAllAsync(int pageindex, int pagesize)
        {
            var user = Db.GetAllAsync();
            var hote = Dbs.GetAllAsync();
            var list = from a in user
                       join b in hote
                       on a.HoteId equals b.HoteId
                       select new
                       {
                           a.UserId,
                           b.HoteId,
                           a.UserName,
                           a.UserPwd,
                           a.UserTime,
                           a.UserAge,
                           a.UserImg,
                           a.UserRuzhi,
                           b.HoteName,
                           a.UserXbie,
                       };
            var count = list.Count();
            var pagecount = list.OrderBy(x=>x.UserId).Skip((pageindex-1)* pagesize).Take(pagesize);
            return new
            {
                count,
                pagecount
            };
        }

        public Task<int> UpdateAsync(Userinfor userinfor)
        {
            return Db.UpdateAsync(userinfor);
        }
    }
}
